﻿using System.Linq;
using System.Web.Mvc;
using Omu.Awesome.Mvc;
using Omu.AwesomeDemo.Core.Service;
using Omu.AwesomeDemo.Infra.Builder;
using Omu.AwesomeDemo.Infra.Dto;
using Omu.AwesomeDemo.Core.Model;

namespace Omu.AwesomeDemo.WebUI.Controllers
{
    public class PersonController : Crudere<Person, PersonCreateInput, PersonEditInput>
    {
        public PersonController(ICrudService<Person> s, IBuilder<Person, PersonCreateInput> v, IBuilder<Person, PersonEditInput> ve) : base(s, v, ve)
        {
        }

        public ActionResult Search(string name, string hobby, int page = 1, int ps = 5)
        {
            var src =
                s.GetAll().Where(o => (o.FirstName + " " + o.LastName + o.Login).ToLower().Contains(name.ToLower())
                                      &&
                                      (string.IsNullOrWhiteSpace(hobby) ||
                                       (o.Hobbies != null && o.Hobbies.Any(h => h.Name.Containz(hobby)))));

            var rows = this.RenderView("rows", src.Skip((page - 1) * ps).Take(ps));

            return Json(new { rows, more = src.Count() > page * ps });
        }
    }
}